// pages/gatherHouse/radarSuccess/radarSuccess.js
import p from "../../../utils/platform";
import api from "../../../common/api";
import { moveHouseLog } from "../../../common/log";
import { menuHandler } from "../../../common/menuHandler";
import { HOUSE_MOVE } from "../../../common/menuId";

const app = getApp();
const errorMsg = {
  5001: "请检查网站是否正确",
};

// 时间延迟
let laybackClear = null;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isLoadHouseDone: false, //加载房源完成
    isLoadingHouse: false, //加载房源中
    model: {},
    url: "",
    houDelId: "",
    subscribe: true,
    feedback: 0, // 1有帮助 2无
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.subscribe) {
      this.setData({
        subscribe: true,
      });
    }
    let info = app.globalData.radarInfo
    app.globalData.radarInfo = null;

    info.content = info.content || {}
    info.content.buildingNumList = info.content.buildingNumList.join(", ");

    // info = {"url":"https://m.ke.com/cwap/guide/hsdetail?cityId=310000&hsId=107104389202","content":{"address":"东安路","beikeResblockId":"5011000017220","buildNum":"中楼层","buildSize":57.13,"buildingDis":"03","buildingNumList":"03","city":"上海市","cityCode":"310000","hall":1,"hdResblockId":"86cd77cceaf74c87a57a639f13c37a8f","houseCode":"107104389202","lat":31.197719,"lng":121.45002,"resblockName":"青松小区","room":1,"roomTypeUrl":"standard_25afe795-2bd9-47c1-a480-55f7490b2e91","roomrardarLogId":"e2ea4de512474d8d80a55aeed3b1ddd3","textStatue":0,"totalFloor":30}}

    this.setData({
      model: info.content,
      url: info.url,
    });
  },

  handleAddHouse() {
    menuHandler.call(this, HOUSE_MOVE).then(() => {
      wx.redirectTo({
        url:
          "/pages/gatherHouse/scanAddHouse/scanAddHouse?scanCode=" + encodeURIComponent(this.data.url) + "&skipMenu=1",
      });
    })
    .catch(() => {});
  },

  addHouse() {
    const result = this.data.url;
    moveHouseLog({
      logType: "1",
      sourcePickOutHouse: result,
    });

    // 为了流畅体验此处需要做个延迟  避免接口发挥过快 页面闪跳
    if (laybackClear) {
      clearTimeout(laybackClear);
      laybackClear = null;
    }

    laybackClear = setTimeout(() => {
      this.setData({
        isLoadingHouse: true,
      });
    }, 500);

    api.scanAddHouseFromWeb({ url: result }, (res) => {
      if (res.data.isSuccess) {
        let houDelId = res.data.content;
        console.log("最新房源代理id", houDelId);

        this.data.houDelId = houDelId;
        this.setData({
          isLoadHouseDone: true,
        });
      } else {
        if (laybackClear) {
          clearTimeout(laybackClear);
          laybackClear = null;
        }
        this.setData({
          isLoadingHouse: false,
        });

        p.showToast({
          title: errorMsg[res.data.errorCode] || res.data.errorMessage || "暂时无法匹配房源图片，请选择其他房源",
          icon: "none",
        });
      }

      let logObj = {
        logType: "2",
        sourcePickOutHouse: result,
        create_result: res.data.isSuccess ? "0" : "1",
      };
      if (res.data.isSuccess) {
        logObj.del_id = this.data.houDelId || "";
      } else {
        logObj.response = res.data.errorMessage || "";
      }

      moveHouseLog(logObj);
    });
  },

  handleLoadHouseDone() {
    this.setData({
      isLoadingHouse: false,
      isLoadHouseDone: false,
    });

    setTimeout(() => {
      if (this.data.houDelId) {
        if (this.data.subscribe) {
          p.redirectTo({
            url: `/pages/addHouse/infoEntryHouse/infoEntryHouse?houDelId=${this.data.houDelId}&isScan=true&subscribe=true`,
          });
        } else {
          p.redirectTo({
            url: `/pages/addHouse/infoEntryHouse/infoEntryHouse?houDelId=${this.data.houDelId}&isScan=true`,
          });
        }
      }
    }, 500);
  },

  timeoutFn() {
    this.setData({
      isLoadingHouse: false,
    });
    p.showToast({
      title: "录房超时！",
      icon: "none",
    });
  },
  back() {
    wx.navigateBack({
      delta: 1,
    });
  },

  handleOk() {
    this.roomRadarFeedBack(true)
  },
  handleNo() {
    this.roomRadarFeedBack(false)
  },

  roomRadarFeedBack(bool) {
    if (this.data.feedback != 0) return
    this.setData({
      feedback: bool ? 1 : 2
    })

    api.roomRadarFeedBack({
      roomrardarLogId: this.data.model.roomrardarLogId,
      feedback: bool
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},
});
